package com.sta.staff.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sta.staff.bean.Result;
import com.sta.staff.pojo.Salary;
import com.sta.staff.pojo.Users;
import com.sta.staff.service.SalaryService;
import com.sta.staff.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/backstage/salary")
public class SalaryController {
    @Autowired
    private SalaryService salaryService;
    @Autowired
    private UsersService usersService;
    //查询所有员工工资
    @RequestMapping("/all")
    public ModelAndView all(){
        ModelAndView modelAndView = new ModelAndView();
        List<Salary> allSalary = salaryService.findAllSalary();

        modelAndView.addObject("allSalary",allSalary);
        modelAndView.setViewName("/backstage/salary_all");
        return modelAndView;
    }
    //查询指定员工工资
    @RequestMapping("/find")
    public ModelAndView find(String search){
        ModelAndView modelAndView = new ModelAndView();
        List<Salary> allSalary = salaryService.findLikeSalary(search);
        modelAndView.addObject("allSalary",allSalary);
        modelAndView.setViewName("/backstage/salary_all");
        return modelAndView;
    }

    //新增员工工资
    @RequestMapping("/add")
    public ModelAndView add(String username,Salary salary){

        ModelAndView modelAndView = new ModelAndView();
        Result result = salaryService.judge(username, salary);
        if (!result.isFlag()) {
            modelAndView.addObject("message", result.getMessage());
            modelAndView.setViewName("/backstage/salary_add");
            return modelAndView;
        }
        Users user = usersService.findByUsersName(username);
        Integer uid = user.getUid();
        salaryService.add(uid,salary);
        modelAndView.setViewName("redirect:/backstage/salary/all");
        return modelAndView;
    }

    //删除员工工资
    @RequestMapping("/delete")
    public String delete(Integer sid){
        salaryService.delete(sid);
        return "redirect:/backstage/salary/all";
    }
    //将需要修改的工作数据回显到修改页面
    @RequestMapping("/edit")
    public ModelAndView edit(Integer sid){
        ModelAndView modelAndView = new ModelAndView();
        Salary salary = salaryService.findBySid(sid);
        modelAndView.addObject("salary",salary);
        modelAndView.setViewName("/backstage/salary_edit");
        return modelAndView;

    }
    //修改员工工资
    @RequestMapping("/update")
    public ModelAndView update(Salary salary){
        ModelAndView modelAndView = new ModelAndView();
        Result result = salaryService.judge2(salary);
        if (!result.isFlag()) {
            modelAndView.addObject("message", result.getMessage());
            modelAndView.setViewName("/backstage/salary_add");
            return modelAndView;
        }
        salaryService.update(salary);
        modelAndView.setViewName("redirect:/backstage/salary/all");
        return modelAndView;
    }
    //查看自己工资
    @RequestMapping("/myAll")
    public ModelAndView findMyAll(){
        // 1.获取会话对象
        SecurityContext context = SecurityContextHolder.getContext();
        // 2.获取认证对象
        Authentication authentication = context.getAuthentication();
        // 3.获取登录用户信息
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String username = userDetails.getUsername();
        Users user = usersService.findMyself(username);
        List<Users> mySalary = salaryService.findMySalary(user.getUid());
        ModelAndView modelAndView = new ModelAndView();
        if (mySalary.size()==0){
            modelAndView.setViewName("/backstage/salary_fail");
            return modelAndView;
        }
        Users users = mySalary.get(0);

        modelAndView.addObject("user",users);
        modelAndView.addObject("mySalary",users.getSalaries());
        modelAndView.setViewName("/backstage/salary_myAll");
        return modelAndView;
    }

}
